package com.leyou.goods.service;

import com.leyou.goods.client.BrandClient;
import com.leyou.goods.client.CategoryClient;
import com.leyou.goods.client.GoodsClient;
import com.leyou.goods.client.SpecificationClient;
import com.leyou.item.pojo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class GoodsService {

    @Autowired
    private GoodsClient goodsClient;

    @Autowired
    private BrandClient brandClient;

    @Autowired
    private CategoryClient categoryClient;

    @Autowired
    private SpecificationClient specificationClient;

    private static final Logger logger = LoggerFactory.getLogger(GoodsService.class);


    public Map<String,Object> buildItemMap(Long spuId) {
        //查询spu
        Spu spu = goodsClient.querySpuById(spuId);
        //查询spuDetail
        SpuDetail spuDetail = goodsClient.querySpuDetailBySpuId(spuId);
        //查询skus
        List<Sku> skus = goodsClient.querySkusBySpuId(spuId);

        //查询categroies
        List<Long> cids = Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3());
        List<String> names = categoryClient.queryNamesByIds(cids);
        List<Category> categories=new ArrayList<>();
        for(int i=0;i<cids.size();i++){
            Category category = new Category();
            category.setId(cids.get(0));
            category.setName(names.get(i));
            categories.add(category);
        }

        //查询brand
        Brand brand = brandClient.queryBrandById(spu.getBrandId());

        //查询特有规格参数
        Map<Long,String> paramMap=new HashMap<>();
        List<SpecParam> params = specificationClient.querySpecParams(null, spu.getCid3(), null, null);
        params.forEach(param -> {
            paramMap.put(param.getId(),param.getName());
        });

        //查询groups
        List<SpecGroup> groups = specificationClient.queryGroupWithParamByCid(spu.getCid3());

        Map<String,Object> map = new HashMap<>();
        map.put("spu",spu);
        map.put("spuDetail",spuDetail);
        map.put("categories",categories);
        map.put("brand",brand);
        map.put("skus",skus);
        map.put("paramMap",paramMap);
        map.put("groups",groups);
        return map;
    }

}
